package com.common.component;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.common.utils.SysConfig;
public class UploadFile extends HttpServlet { 
   private static final long serialVersionUID = 5425836142860976977L;
   /** *//** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. 
     * @param request servlet request 
     * @param response servlet response 
     */ 
    // 定义文件的上传路径 
    private String uploadPath ; 
    // 限制文件的上传大小 
    public UploadFile() { 
        super(); 
    } 
    public void destroy() { 
        super.destroy(); 
    } 
   /* protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        System.out.println("Access !"); 
        response.setContentType("text/html;charset=UTF-8"); 
        uploadPath = "D:\\files\\";//SysConfig.imgRealPath;
        //保存文件到服务器中 
        DiskFileItemFactory factory = new DiskFileItemFactory(); 
        factory.setSizeThreshold(4096); 
        ServletFileUpload upload = new ServletFileUpload(factory); 
        upload.setHeaderEncoding("utf-8");
        upload.setSizeMax(maxPostSize); 
        try { 
           List fileItems = upload.parseRequest(request); 
           Iterator iter = fileItems.iterator(); 
           while (iter.hasNext()) { 
               FileItem item = (FileItem) iter.next(); 
               if (!item.isFormField()) { 
                   String name = item.getName();
                   String newName =DateUtil.getVersionSequence()+""+name.substring(name.lastIndexOf("."));
                   try { 
                	   System.out.println(newName);
                       item.write(new File(uploadPath+newName)); 
                   } catch (Exception e) { 
                       e.printStackTrace(); 
                       response.getWriter().write(e.getMessage());
                   } 
               } 
           } 
       } catch (FileUploadException e) { 
           e.printStackTrace(); 
           response.getWriter().write(e.getMessage());
       } 
   } */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) 
           throws ServletException, IOException { 
       processRequest(request, response); 
   } 
   /** *//** 
    * Handles the HTTP <code>POST</code> method. 
    * @param request servlet request 
    * @param response servlet response 
    */ 
   protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        processRequest(request, response); 
    } 
    private void processRequest(HttpServletRequest request,
		HttpServletResponse response) {
    	response.setCharacterEncoding("utf-8");
    	 uploadPath = SysConfig.imgRealPath;
    	 String newFileName= request.getParameter("newName");
    	CommonsMultipartResolver commonsMultipartResolver =    
            new CommonsMultipartResolver(request.getSession().getServletContext());   
        commonsMultipartResolver.setDefaultEncoding("utf-8");   
        if (commonsMultipartResolver.isMultipart(request)) {     
            MultipartHttpServletRequest multipartRequest =    
                commonsMultipartResolver.resolveMultipart(request);     
      
            Iterator iter = multipartRequest.getFileNames();   
            for (;iter.hasNext();) {   
                MultipartFile file = multipartRequest.getFile((String)iter.next());   
                if (file != null) {   
                	String filePath=uploadPath+file.getOriginalFilename();
                	if(newFileName!=null && !"".equals(newFileName)){
                		filePath = uploadPath+newFileName;
                	}
                	System.out.println("执行文件上传操作:="+filePath);
                	File localFile = new File(filePath);
                    try {
						file.transferTo(localFile);
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}   
                }   
            }
        }

}
	/** *//** 
     * Returns a short description of the servlet. 
     */ 
    public String getServletInfo() { 
        return "Short description"; 
    } 
}